home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / binaries / Windows / jsdk / src / sun / servlet / netscape / NSResponse.java < prev    next >
Encoding:
Java Source  |  1997-07-18  |  7.3 KB  |  273 lines

  1. /*
  2.  * @(#)NSResponse.java    1.9 97/05/22
  3.  * 
  4.  * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  * CopyrightVersion 1.0
  20.  */
  21.  
  22. package sun.servlet.netscape;
  23.  
  24. import java.io.IOException;
  25. import java.io.PrintStream;
  26. import java.util.Date;
  27. import java.util.Observer;
  28. import java.util.Observable;
  29. import javax.servlet.ServletOutputStream;
  30. import javax.servlet.http.HttpServletResponse;
  31. import sun.servlet.http.HttpOutputStream;
  32. import netscape.server.applet.ServerApplet;
  33. import netscape.server.applet.Server;
  34.  
  35. /*
  36.  * This class represents a servlet response for servlets running in Netscape's
  37.  * Enterprise or FastTrack servers. The response object is passed to the
  38.  * servlet by a special server applet.
  39.  *
  40.  * @version    1.9, 05/22/97
  41.  * @author    David Connelly
  42.  */
  43. public
  44. class NSResponse implements HttpServletResponse, Observer {
  45.     /*
  46.      * The servlet output stream for this response.
  47.      */
  48.     private HttpOutputStream out = new HttpOutputStream();
  49.  
  50.     /*
  51.      * The servlet runner for this response.
  52.      */
  53.     private NSRunner sr;
  54.  
  55.     /*
  56.      * Initializes this response.
  57.      */
  58.     void init(NSRunner sr) throws IOException {
  59.     this.sr = sr;
  60.     out.init(sr.getOutputStream());
  61.     out.next();
  62.     out.setObserver(this);
  63.     }
  64.  
  65.     /**
  66.      * Returns true if the specified header field is contained in this
  67.      * response otherwise returns false.
  68.      * @param name the header field name
  69.      */
  70.     public boolean containsHeader(String name) {
  71.     return sr.getResponseProperty(name.toLowerCase()) != null;
  72.     }
  73.  
  74.     /*
  75.      * Resets the response.
  76.      */
  77.     void reset() {
  78.     this.sr = null;
  79.     out.resets();
  80.     }
  81.  
  82.     /*
  83.      * Finishes the response.
  84.      */
  85.     void finish() throws IOException {
  86.     out.finish();
  87.     }
  88.  
  89.     /**
  90.      * Sets the content length of the response.
  91.      * @param len the content length
  92.      */
  93.     public void setContentLength(int len) {
  94.     sr.setResponseProperty("content-length", Integer.toString(len));
  95.     out.setContentLength(len);
  96.     }
  97.  
  98.     /**
  99.      * Sets the content type for the response.
  100.      * @param type the content type
  101.      */
  102.     public void setContentType(String type) {
  103.     sr.setResponseProperty("content-type", type);
  104.     }
  105.  
  106.     /**
  107.      * Returns the servlet output stream for writing response data.
  108.      * @exception IOException if an I/O error has occurred
  109.      */
  110.     public ServletOutputStream getOutputStream() {
  111.     return out;
  112.     }
  113.  
  114.     /**
  115.      * Sets the status code and reason phrase for the response.
  116.      * @param code the status code
  117.      * @param reason the reason phrase
  118.      */
  119.     public void setStatus(int code, String reason) {
  120.     sr.setStatus(code, reason);
  121.     }
  122.  
  123.     /**
  124.      * Sets the status code of the response with a default reason phrase.
  125.      * @param code the status code
  126.      */
  127.     public void setStatus(int code) {
  128.     sr.setStatus(code);
  129.     }
  130.  
  131.     /**
  132.      * Sets a response header.
  133.      * @param name the header name
  134.      * @param value the header value
  135.      */
  136.     public void setHeader(String name, String value) {
  137.     sr.setResponseProperty(name.toLowerCase(), value);
  138.     }
  139.  
  140.     /**
  141.      * Sets an integer response header.
  142.      * @param name the header name
  143.      * @param value the header integer value
  144.      */
  145.     public void setIntHeader(String name, int value) {
  146.     sr.setResponseProperty(name.toLowerCase(), Integer.toString(value));
  147.     }
  148.  
  149.     /**
  150.      * Sets a date response header.
  151.      * @param name the header name
  152.      * @param value the header date value
  153.      */
  154.     public void setDateHeader(String name, long value) {
  155.     sr.setResponseProperty(name.toLowerCase(), new Date(value).toString());
  156.     }
  157.  
  158.     /**
  159.      * Sends an error response using the specified status code and detail
  160.      * message.
  161.      * @param status the status code
  162.      */
  163.     public void sendError(int status) throws IOException {
  164.     sendError(status, status + " " + reason(status));
  165.     }
  166.  
  167.     /**
  168.      * Sends an error response using the specified status code and detail
  169.      * message.
  170.      * @param status the status code
  171.      * @param detail the error message
  172.      */
  173.     public void sendError(int status, String detail) throws IOException {
  174.     if (out.getTotal() > 0) {
  175.         return;
  176.     }
  177.         if (sr.returnErrorResponse("text/html", status, reason(status))) {
  178.         PrintStream out = sr.getOutputStream();
  179.         out.println("<html>");
  180.         out.println("<title>" + detail + "</title>");
  181.         out.println(detail);
  182.         out.println("</html>");
  183.         out.flush();
  184.         }
  185.     }
  186.  
  187.     /**
  188.      * Sends a redirect response to the client using the specified redirect
  189.      * location URL.
  190.      * @param location the redirect location
  191.      */
  192.     public void sendRedirect(String location) throws IOException {
  193.     if (out.getTotal() > 0) {
  194.         return;
  195.     }
  196.     sr.setResponseProperty("location", location);
  197.     if (sr.returnErrorResponse("text/html",
  198.                    sr.REDIRECT, reason(sr.REDIRECT))) {
  199.         PrintStream out = sr.getOutputStream();
  200.         out.println("<html><title>Document moved</title>/</html>");
  201.         out.println("<body><h1>Document moved</h1>");
  202.         out.print("This document has moved <a href=\"");
  203.         out.print(location);
  204.         out.println("\">here</a>.<p></body>");
  205.         out.flush();
  206.     }
  207.     }
  208.  
  209.     /*
  210.      * Returns reason phrase message for specified status code.
  211.      */
  212.     private static String reason(int status) {
  213.     switch (status) {
  214.       case SC_OK:
  215.         return "OK";
  216.       case SC_CREATED:
  217.         return "Created";
  218.       case SC_ACCEPTED:
  219.         return "Accepted";
  220.       case SC_NO_CONTENT:
  221.         return "No Content";
  222.       case SC_MOVED_PERMANENTLY:
  223.         return "Moved Permanently";
  224.       case SC_MOVED_TEMPORARILY:
  225.         return "Moved Temporarily";
  226.       case SC_NOT_MODIFIED:
  227.         return "Not Modified";
  228.       case SC_BAD_REQUEST:
  229.         return "Bad Request";
  230.       case SC_UNAUTHORIZED:
  231.         return "Unauthorized";
  232.       case SC_FORBIDDEN:
  233.         return "Forbidden";
  234.       case SC_NOT_FOUND:
  235.         return "Not Found";
  236.       case SC_INTERNAL_SERVER_ERROR:
  237.         return "Internal Server Error";
  238.       case SC_NOT_IMPLEMENTED:
  239.         return "Not Implemented";
  240.       case SC_BAD_GATEWAY:
  241.         return "Bad Gateway";
  242.       case SC_SERVICE_UNAVAILABLE:
  243.         return "Service Unavailable";
  244.       default:
  245.         return "Unknown";
  246.         }
  247.     }
  248.  
  249.     /**
  250.      * Notifies response object when output stream has been first written.
  251.      */
  252.     public void update(Observable obs, Object obj) {
  253.       HttpOutputStream out;
  254.  
  255.       try {
  256.     out = (HttpOutputStream)obj;
  257.       } catch (ClassCastException e) {
  258.     throw new InternalError("update called with invalid argument type");
  259.       }
  260.       try {
  261.     if (!containsHeader("status")) { //yes, it isn't a header...
  262.       setStatus(SC_OK,reason(SC_OK));
  263.     }
  264.     if (!containsHeader("content-type")) {
  265.       setContentType("text/html");
  266.     }
  267.     sr.startResponse();
  268.       } catch (IOException e) {
  269.     out.setIOException(e);
  270.       }
  271.     }
  272. }
  273.